package com.guigu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.base.entity.CourierScop;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.entity.GlobalUser;
import com.guigu.base.mapper.CourierScopMapper;
import com.guigu.base.service.CourierScopService;
import com.guigu.base.service.GlobalUserService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 快递员业务范围表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@Service
public class CourierScopServiceImpl extends ServiceImpl<CourierScopMapper, CourierScop> implements CourierScopService {

    private GlobalUserService globalUserService;

    public CourierScopServiceImpl(GlobalUserService globalUserService) {
        this.globalUserService = globalUserService;
    }

    /**
     * 根据行政区域Id获取当前行政区域范围内的快递员
     *
     * @param areaId
     * @return
     */
    @Override
    public List<CourierScop> getCourierScopeInfo(String areaId) {
        if (StringUtils.isBlank(areaId)) {
            throw new TmsException(ResultCode.ERROR_CODE, "行政区域Id不能为null");
        }
        QueryWrapper<CourierScop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_id", areaId);
        List<CourierScop> courierScops = this.baseMapper.selectList(queryWrapper);
        return courierScops;
    }

    /**
     * 批量保存快递员业务范围
     *
     * @param scopeList 快递员业务范围信息列表
     */
    @Override
    public void batchSave(List<CourierScop> scopeList) {
        saveBatch(scopeList);
    }

    /**
     * 删除快递员业务范围
     *
     * @param areaId 行政区域id
     * @param userId 快递员id
     */
    @Override
    public void delete(String areaId, String userId) {
        LambdaQueryWrapper<CourierScop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        boolean canExecute = false;
        if (StringUtils.isNotEmpty(areaId)) {
            lambdaQueryWrapper.eq(CourierScop::getAreaId, areaId);
            canExecute = true;
        }
        if (StringUtils.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(CourierScop::getUserId, userId);
            canExecute = true;
        }
        if (canExecute) {
            baseMapper.delete(lambdaQueryWrapper);
        }
    }

    @Override
    public List<CourierScop> findAll(String areaId, String userId) {
        LambdaQueryWrapper<CourierScop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(areaId)) {
            lambdaQueryWrapper.eq(CourierScop::getAreaId, areaId);
        }
        if (StringUtils.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(CourierScop::getUserId, userId);
        }
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据机构Id查询它对应的快递员信息
     *
     * @param coreOrgId 机构Id
     * @return
     */
    @Override
    public R<List<GlobalUser>> findGlobalUserByCoreOrgId(String coreOrgId) {
        List<GlobalUser> list = new ArrayList<>();
        QueryWrapper<CourierScop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_id", coreOrgId);
        /**
         * 查出快递员业务范围集合
         */
        List<CourierScop> courierScops = this.baseMapper.selectList(queryWrapper);

        /**
         * 循环查找所对应的快递员信息
         */
        courierScops.stream().forEach((e) -> {
            GlobalUser user = globalUserService.getById(e.getUserId());
            if (ObjectUtils.isNotEmpty(user)) {
                list.add(user);
            }
        });
        return new R<>(ResultCode.SUCCESS_CODE, list);
    }
}
